<?php

use yii\bootstrap\ActiveForm;
use common\helps\tools;

?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/select2/select2.css" type="text/css">
<style>
    .examine-content .input-group .form-control {
        margin-bottom: 20px;
        height: 30px;
        line-height: 30px;
    }

    .examine-content button {
        margin: 10px;
    }

    label {
        text-align: right;
    }

    .modal-body > form > div.col-md-12 {
        padding-bottom: 20px
    }
    .imgBox img{ height: 100px; margin-top: 10px; border: 1px solid #ddd; padding: 2px ; float: left}
    .imgBox div{ position: relative; float: left; margin-top: 30px; padding: 0 10px}
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <div class="modal-body">
                        <?php $form = ActiveForm::begin(['id' => 'add_supplier_form', 'action' => ['/supplier/add-supplier']]); ?>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">供应商名称</label>
                            <div class="col-sm-4">
                                <input name="id" id="id" type="hidden" value="<?= $supplier ? $supplier['id'] : '' ?>">
                                <input type="text" class="form-control" name="supplierName" id="supplierName"
                                       value="<?= $supplier ? $supplier['supplierName'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">省</label>
                            <div class="col-sm-4">
                                <select id="province" name="province" class="form-control">
                                    <option value="0">请选择</option>
                                    <?php foreach ($province as $value) { ?>
                                        <option value="<?= $value['ProvinceID'] ?>" <?= ($supplier && $supplier['province'] == $value['ProvinceName']) ? 'selected' : '' ?>><?= $value['ProvinceName'] ?></option>
                                    <?php } ?>
                                </select>
                                <!--<input type="text" class="form-control" name="province" id="province" value="<? /*= $supplier?$supplier['province']:'' */ ?>">-->
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">市</label>
                            <div class="col-sm-4">
                                <select id="city" name="city" class="form-control"></select>
                            </div>
                            <label class="col-sm-2 control-label">区</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="area" id="area"
                                       value="<?= $supplier ? $supplier['area'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">地址</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="address" id="address"
                                       value="<?= $supplier ? $supplier['address'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">联系人</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="contacts" id="contacts"
                                       value="<?= $supplier ? $supplier['contacts'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">联系手机</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="mobile" id="mobile"
                                       value="<?= $supplier ? $supplier['mobile'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">联系电话</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="phone" id="phone"
                                       value="<?= $supplier ? $supplier['phone'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">经营范围</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="scope" id="scope"
                                       value="<?= $supplier ? $supplier['scope'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">开户行</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="bank" id="bank"
                                       value="<?= $supplier ? $supplier['bank'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">银行账号</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="account" id="account"
                                       value="<?= $supplier ? $supplier['account'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">税务登记证</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="taxRegistration" id="taxRegistration"
                                       value="<?= $supplier ? $supplier['taxRegistration'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">组织机构代码</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="organization" id="organization"
                                       value="<?= $supplier ? $supplier['organization'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">注册地址</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="registerAdd" id="registerAdd"
                                       value="<?= $supplier ? $supplier['registerAdd'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">注册电话</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="registerPhone" id="registerPhone"
                                       value="<?= $supplier ? $supplier['registerPhone'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">账期描述</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="payCycle" id="payCycle"
                                       value="<?= $supplier ? $supplier['payCycle'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>上传图片</label>
                            <div class="col-sm-9">
                                <button id="uploadBtn" type="button" class="btn btn-success depart-modal" style="width: 120px;">上传图片</button>
                                <div class="imgBox">
                                    <?php if($supplier && $supplier['imgArr']){ foreach ($supplier['imgArr'] as $key=>$img) { ?>
                                        <img src="/images/<?=$img?>">
                                        <div>
                                            <a name="delImg" data-id="<?=$key?>">删除</a>
                                            <br><br>
                                            <a href="/images/<?=$img?>" download="">下载</a>
                                        </div>
                                    <?php } } ?>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">备注</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="remark" id="remark"
                                       value="<?= $supplier ? $supplier['remark'] : '' ?>">
                            </div>
                        </div>
                        <?php ActiveForm::end(); ?>
                    </div>
                    <form id="uploadform" action="/supplier/img-upload" method="post" role="form" enctype="multipart/form-data" style="display: none">
                        <input type="file" id="uploadFile" name="uploadFile">
                    </form>
                    <div class="modal-footer" style="border: 0">
                        <button type="button" class="btn btn-default" onclick="history.back();">返回
                        </button>
                        <a type="button" class="btn btn-primary ajax_post_add_supplier"> 提交 </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <input id="old_id" value="" type="hidden"/>
    <input name="class_len" id="class_len" value="0" type="hidden"/>
    <input name="class_len_2" id="class_len_2" value="0" type="hidden"/>
    <input type="hidden" name="imageBathUrl" id="imageBathUrl"
           value="<?php echo Yii::$app->params['imageBathUrl'] . '/' ?>">
    <script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
    <script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
    <script type="text/javascript" src="/static/plugins/xheditor/xheditor-1.2.2.min.js"></script>
    <script type="text/javascript" src="/static/plugins/xheditor/xheditor_lang/zh-cn.js"></script>
    <script type="text/javascript" src="../static/plugins/jquery.form.js"></script>
    <script type="text/javascript" src="../static/plugins/layer/layer.js"></script>
    <script type="text/javascript" src="../static/plugins/select2/select2.js"></script>
    <!-- 自定义js -->

    <script>
        var city = <?=$city ?>;
        var imgArr = <?= $supplier && $supplier['imgArr'] ? json_encode($supplier['imgArr']) : '{}' ?>;
        $(function () {
            $('#province').on('change', function () {
                changeProvince();
            });
            changeProvince('<?=$supplier ? $supplier['city'] : '' ?>');



            $("#province").select2();

        });

        function changeProvince(sname) {
            var cityArr = city[$('#province').val()];
            var selectVal = 0;


            var cityData = [{"id": 0, "text": "请选择"}];
            $.each(cityArr, function (i, v) {
                cityData.push( {"id": v.CityID,"text": v.CityName } );
                if(sname == v.CityName)
                {
                    selectVal = v.CityID;
                }
            });

            $('#city').empty();
            var selectCity = $('#city').select2({
                data: cityData
            });
            console.log(selectVal);
            selectCity.val(selectVal).trigger('change');
        }

        $(".ajax_post_add_supplier").on('click', function () {

            var supplierName = $('#supplierName').val();
            var provinceID = $("#province").select2("val");
            var cityID = $("#city").select2("val");


            var area = $('#area').val();
            var address = $('#address').val();
            var contacts = $('#contacts').val();
            var mobile = $('#mobile').val();
            var phone = $('#phone').val();
            var scope = $('#scope').val();
            var bank = $('#bank').val();
            var account = $('#account').val();
            var taxRegistration = $('#taxRegistration').val();
            var organization = $('#organization').val();
            var registerAdd = $('#registerAdd').val();
            var registerPhone = $('#registerPhone').val();
            var payCycle = $('#payCycle').val();
            var remark = $('#remark').val();

            if (supplierName == '') {
                $.jBox.error('请填写供应商名称', '错误');
                return false;
            }
            if (provinceID == 0 || provinceID == 0) {
                $.jBox.error('请选择省份', '错误');
                return false;
            }
            if (cityID == '' || cityID == 0) {
                $.jBox.error('请选择城市', '错误');
                return false;
            }
            if (area == '') {
                $.jBox.error('请选择区域', '错误');
                return false;
            }
            var province = $('#province').select2('data')[0].text;
            var city = $('#city').select2('data')[0].text;


            var param = {
                id: $('#id').val(),
                supplierName: supplierName,
                province: province,
                city: city,
                area: area,
                address: address,
                contacts: contacts,
                mobile: mobile,
                phone: phone,
                remark: remark,
                scope: scope,
                bank: bank,
                account: account,
                taxRegistration: taxRegistration,
                organization: organization,
                registerAdd: registerAdd,
                registerPhone: registerPhone,
                payCycle: payCycle,
                imgArr: imgArr
            };
            $.post(
                $('#add_supplier_form').attr('action'),
                param,
                function (data) {
                    if (data.code == 200) {
                        $.jBox.tip('处理成功', 'success');
                        window.setTimeout(function () {
                            history.back();
                        }, 1000);
                    } else {
                        $.jBox.error(data.val, '错误');
                    }
                },
                'json'
            );

        });

        //上传图片
        $('#uploadBtn').click(function(){ $('#uploadFile').trigger('click'); });
        $('#uploadFile').change(function() { if(this.value){ $('#uploadform').submit(); }});
        $('#uploadform').submit(function(e) {
            e.preventDefault();
            $(this).ajaxSubmit({
                success:function(o) {
                    var o = $.parseJSON(o);
                    if (o.code == 200) {
                        var html = '<img src="' + o.data.src + '">';
                        html += '<div>';
                        html += '  <a name="delImg" data-id="' + o.data.key + '">删除</a>';
                        html += '  <br><br>';
                        html += '<a href="' + o.data.src + '" download="">下载</a>';
                        html += '</div>';
                        $('.imgBox').append(html);
                        imgArr[o.data.key] = o.data.url;
                    } else {
                        $.jBox.error(o.val, '错误');
                    }
                }
            });
        });

        $(document).on('click','a[name=delImg]', function () {
            delete imgArr[$(this).attr('data-id')];
            $(this).parent().prev().remove();
            $(this).parent().remove();
        })

    </script>